package com.wisdytech.linkdcs.factory.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.account.dao.TtEqAccountDao;
import com.wisdytech.linkdcs.account.model.TtEqAccount;
import com.wisdytech.linkdcs.factory.dao.TmAreaDao;
import com.wisdytech.linkdcs.factory.model.Area;
import com.wisdytech.linkdcs.factory.model.ProductionLine;
import com.wisdytech.linkdcs.factory.model.Shop;
import com.wisdytech.linkdcs.factory.service.TmAreaService;
import com.wisdytech.linkdcs.system.model.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * ClassName: TmAreaServiceImpl
 * <p>
 * Description: 区域管理的业务实现类
 * </p>
 * <p>
 * Author: chenbin
 * </p>
 * <p>
 * Date: 2018年5月7日
 * </p>
 */
@Service("tmAreaService")
@Transactional
public class TmAreaServiceImpl extends BaseService<TmAreaDao, Area> implements TmAreaService{

    /**
     * 注入区域管理dao
     */
	@Resource
	private TmAreaDao tmAreaDao;
    /**
     * 注入设备台账dao
     */
	@Resource
	private TtEqAccountDao ttEqAccountDao;
	
	@Override
	public PageInfo<Area> listAreaByFilter(String locationCode, String locationName, Filter filter) {
        //查询所有产线
		Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("locationCode", locationCode);
        queryMap.put("locationName", locationName);
        List<Area> lists =
                super.beforeList(TmAreaDao.class, filter).listAreaByFilter(queryMap);
        PageInfo<Area> AreaPage = new PageInfo<>(lists);
        return AreaPage;
	}

	@Override
	public boolean addArea(Area area, String loginName) {
		EntityWrapper<Area> ew = new EntityWrapper<>();
        //验证车间编码的唯一性
        List<Area> areaList = new ArrayList<>();
        areaList = tmAreaDao.selectPage(new Page<SysUser>(1, 1), ew.eq("LOCATION_CODE", area.getLocationCode()).eq("del_flag", CommonConstants.DEL_FLAG));
        if (!areaList.isEmpty()) {
        	throw new ServiceException("该区域编码已被使用");
        }
        
        area.setPkId(BaseUtil.getUUID());
        area.setDelFlag(CommonConstants.DEL_FLAG);
        area.setCreateUser(loginName);
        area.setCreateDate(new Date());
        int operationCount = tmAreaDao.insert(area);
        if (operationCount <= 0) {
            throw new ServiceException("新增失败");
        }
        return true;
	}

	@Override
	public Area getAreaById(String pkId) {
		if(StringUtils.isEmpty(pkId)) {
			return new Area();
		}
		return tmAreaDao.selectById(pkId);
	}

	@Override
	public boolean updateArea(Area area, String loginName) {
        EntityWrapper<Area> ew =new EntityWrapper<>();
        //验证车间编号的唯一性
        List<Area> areaList;
        areaList = tmAreaDao.selectPage(new Page<Area>(1, 1), ew.eq("LOCATION_CODE", area.getLocationCode()).ne("pk_id", area.getPkId()).eq("del_flag", CommonConstants.DEL_FLAG));
        if (!areaList.isEmpty()) {
        	throw new ServiceException("该区域编码已被使用");
        }
		tmAreaDao.updateAreaById(area);
		return true;
	}

	@Override
	public boolean delAreaById(String ids) {
        List<String> idList = new ArrayList<String>();
        String[] id = ids.split(",");
        for (int i = 0; i < id.length; i++) {
            idList.add(id[i]);
        }
        for (String pkId : id) {
            //通过车间的id判断车间下是否有设备
        	EntityWrapper<TtEqAccount> ew = new EntityWrapper<>();
        	ew.eq("LOCATION_ID", pkId);
        	int count = ttEqAccountDao.selectCount(ew);
            if (count>0) {
            	 throw new ServiceException("所选区域下存在“设备”");
            }
        }
        
        int factoryCount = tmAreaDao.deleteBatchIds(idList);
        if (factoryCount <= 0) {
            throw new ServiceException("删除失败");
        }
        return true;
	}

    @Override
    public List<Area> listAllArea(String factoryId) {
	    EntityWrapper<Area> ew = new EntityWrapper<>();
	    ew.setSqlSelect("pk_id","location_code","location_name");
	    ew.eq("del_flag",CommonConstants.DEL_FLAG);
	    if(StringUtils.isNotBlank(factoryId)) {
	        ew.eq("factory_id",factoryId);
        }
        return tmAreaDao.selectList(ew);
    }
}
